<template>
  <el-dialog
    :title="'选择商铺'"
    :append-to-body="true"
    :close-on-click-modal="isShow"
    :visible.sync="visible">
    <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="商铺名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="cid"
        v-if="false"
        header-align="center"
        align="center"
        width="80"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="shopName"
        header-align="center"
        align="center"
        label="商铺名">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="Logo">
        <template slot-scope="scope">
          <img :src="openImg(scope.row.shopLogoUrl)" width="150px" height="50px"/>
        </template>
      </el-table-column>
      <el-table-column
        prop="operatorName"
        header-align="center"
        align="center"
        label="营业人">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="营业执照">
        <template slot-scope="scope">
          <img :src="openImg(scope.row.businessLicenseUrl)" width="150px" height="50px"/>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="chooseHandle(scope.row.cid, scope.row.shopName)">选择</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        isShow: false,
        dataForm: {
          name: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: []
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.isShow = true
        this.visible = true
        this.$nextTick(() => {
          this.dataListLoading = true
          this.$http({
            url: this.$http.adornUrl('/pig-center-biz/collect/company/lists'),
            method: 'get',
            params: this.$http.adornParams({
              'page': this.pageIndex,
              'limit': this.pageSize,
              'name': this.dataForm.name
            })
          }).then(({data}) => {
            console.log(data)
            if (data && data.code === 0) {
              this.dataList = data.page.list
              this.totalPage = data.page.totalCount
            } else {
              this.dataList = []
              this.totalPage = 0
              this.$message.error(data.msg)
            }
            this.dataListLoading = false
          })
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      openImg (url) {
        if (url) {
          url = this.$http.adornUrl(`/pig-file/center/file/openImg?url=${url}`)
          return url
        } else {
          return null
        }
      },
      chooseHandle (id, name) {
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500,
          onClose: () => {
            this.visible = false
            this.$emit('refreshDataList', id, name)
          }
        })
      }
    }
  }
</script>

<style>
</style>
